#encoding: utf-8
require 'nkf'
require 'iconv'
class StockDailyDetail < ActiveRecord::Base

  before_create :init_create

  protected
  def init_create
  end

  public
  def count_daily_detail()
    small_buy_order = 0
    small_sell_order = 0
    large_buy_order = 0
    large_sell_order = 0
    middle_order = 0
    total_order = 0
    self.daily_detail.to_s.split("\n").each { |row|
      datas = row.split(",")
      total_order += datas[4].to_i
    }
    average_order = total_order / self.daily_detail.to_s.split("\n").length
    self.daily_detail.to_s.split("\n").each { |row|
      datas = row.split(",")
      #puts datas[5].decode('ASCII-8BIT')
      #puts Iconv.iconv("utf-8","gbk",datas[5])
      #puts '买盘'.chars ==  datas[5].chars
      #puts datas[5].encoding
      #puts Iconv.iconv("utf-8","gbk",datas[5])[0]
#      if datas[4].to_i < 100000 and Iconv.iconv("utf-8","gbk",datas[5])[0] == '买盘'
#        small_buy_order += datas[4].to_i
#      end
      if Iconv.iconv("utf-8", "gbk", datas[5])[0] == '买盘'
        if datas[4].to_i < 100000
          small_buy_order += datas[4].to_i
        else
          large_buy_order += datas[4].to_i
        end
      elsif Iconv.iconv("utf-8", "gbk", datas[5])[0] == '卖盘'
        if datas[4].to_i < 100000
          small_sell_order += datas[4].to_i
        else
          large_sell_order += datas[4].to_i
        end
      else
        middle_order += datas[4].to_i
      end
    }
    puts "小买单：#{small_buy_order}"
    puts "小卖单：#{small_sell_order}"
    puts "大买单：#{large_buy_order}"
    puts "大卖单：#{large_sell_order}"
    puts "中性盘：#{middle_order}"
    puts "总交易额：#{total_order}"
    puts "平均价：#{average_order}"
  end
end
